/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import cn.hutool.core.lang.UUID;
import me.zhengjie.modules.system.domain.TermsRules;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.TermsRulesRepository;
import me.zhengjie.modules.system.service.TermsRulesService;
import me.zhengjie.modules.system.service.dto.TermsRulesDto;
import me.zhengjie.modules.system.service.dto.TermsRulesQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.TermsRulesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2025-08-26
**/
@Service
@RequiredArgsConstructor
public class TermsRulesServiceImpl implements TermsRulesService {

    private final TermsRulesRepository termsRulesRepository;
    private final TermsRulesMapper termsRulesMapper;

    @Override
    public Map<String,Object> queryAll(TermsRulesQueryCriteria criteria, Pageable pageable){
        Page<TermsRules> page = termsRulesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(termsRulesMapper::toDto));
    }

    @Override
    public List<TermsRulesDto> queryAll(TermsRulesQueryCriteria criteria){
        return termsRulesMapper.toDto(termsRulesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TermsRulesDto findById(Long id) {
        TermsRules termsRules = termsRulesRepository.findById(id).orElseGet(TermsRules::new);
        ValidationUtil.isNull(termsRules.getId(),"TermsRules","id",id);
        return termsRulesMapper.toDto(termsRules);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TermsRulesDto create(TermsRules resources) {
        resources.setGmtCreate(DateUtil.TimestampNow());
        resources.setGmtModified(DateUtil.TimestampNow());
        resources.setCreateby(SecurityUtils.getCurrentUsername());
        resources.setRuleCode(UUID.fastUUID().toString(true));
        return termsRulesMapper.toDto(termsRulesRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TermsRules resources) {
        TermsRules termsRules = termsRulesRepository.findById(resources.getId()).orElseGet(TermsRules::new);
        ValidationUtil.isNull( termsRules.getId(),"TermsRules","id",resources.getId());
        termsRules.copy(resources);
        termsRules.setGmtModified(DateUtil.TimestampNow());
        termsRules.setUpdateby(SecurityUtils.getCurrentUsername());
        termsRulesRepository.save(termsRules);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            termsRulesRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TermsRulesDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TermsRulesDto termsRules : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("条款编号", termsRules.getRuleCode());
            map.put("条款标题", termsRules.getTitle());
            map.put("条款内容", termsRules.getContent());
            map.put("是否生效0：无效，1：生效，-1：删除", termsRules.getStatus());
            map.put("创建时间", termsRules.getGmtCreate());
            map.put("修改时间", termsRules.getGmtModified());
            map.put("创建人", termsRules.getCreateby());
            map.put("修改人", termsRules.getUpdateby());
            map.put("版本号", termsRules.getVersion());
            map.put("排序", termsRules.getSort());
            map.put("类型", termsRules.getType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}